﻿#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <QMainWindow>
#include <QLabel>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostInfo>
#include <QHostAddress>

namespace Ui {
class TcpServer;
}

class TcpServer : public QMainWindow
{
    Q_OBJECT

public:
    explicit TcpServer(QWidget *parent = nullptr);
    ~TcpServer();

private:
    // 初始化Ui
    void initUi();

    // 获取本机Ip地址
    QString getLocalIp();

private:
    // 状态栏标签
    QLabel* labListen;
    QLabel* labSocketState;

    // TCP服务器
    QTcpServer* tcpServer;
    // TCP通信的Socket
    QTcpSocket* tcpSocket;


protected:
    void closeEvent(QCloseEvent* event);

private slots:
    // QTcpServer的newConnection()信号
    void onNewConnection();

    void onSocketStateChange(QAbstractSocket::SocketState socketState);

    // Client connected
    void onClientConnected();

    // Client disconnected
    void onClientDisconnected();

    // 读取Socket传入的数据
    void onSocketReadyRead();

private slots:
    /**
     * @brief onStartListenAct 菜单按钮触发，开始监听
     */
    void onStartListenAct();

    /**
     * @brief onStopListenAct 菜单按钮触发，停止监听
     */
    void onStopListenAct();

    /**
     * @brief onClearAct 菜单按钮触发，清空文本框
     */
    void onClearAct();

    // 退出软件
    void onQuitAct();

    // 发送消息
    void on_btnSend_clicked();

private:
    Ui::TcpServer *ui;

    // action
    QAction *startListenAct;
    QAction *stopListenAct;
    QAction *clearAct;
    QAction *quitAct;
};

#endif // TCPSERVER_H
